package com.kgc.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kgc.scd.bean.ShopAddress;
import com.kgc.scd.bean.ShopUser;
import com.kgc.scd.mapper.ShopAddressMapper;
import com.kgc.scd.mapper.ShopUserMapper;
import com.kgc.scd.service.ModifyShopUserService;
import com.kgc.scd.util.EncodeUtils;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.SnowUtil;
import com.kgc.scd.vo.ShopAddressVO;
import com.kgc.scd.vo.ShopUserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created On : 2022/11/16.
 * <p>
 * Author : 刘慧枫
 * <p>
 * Description: 修改用户个人信息接口实现类
 */
@Slf4j
@Service
public class ModifyShopUserServiceImpl implements ModifyShopUserService {

    @Autowired(required = false)
    private ShopUserMapper shopUserMapper;

    @Autowired(required = false)
    private ShopAddressMapper shopAddressMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private SnowUtil snowUtil;

    @Override
    public ShopUserVO modShopUserInfo(Integer gender, Date birthday, String nickname, String headImg, ShopUser shopUser) {

        //保存数据库
        shopUser.setGender(gender);
        shopUser.setBirthday(birthday);
        shopUser.setNickname(nickname);
        shopUser.setHeadImg(headImg);
        shopUserMapper.updateById(shopUser);

        //id生成令牌 更新redis
        String userToken = EncodeUtils.testDigest(shopUser.getId());
        ShopUser shopUser1 = shopUserMapper.selectById(shopUser.getId());
        redisUtils.set(userToken,shopUser1);

        ShopUserVO shopUserVO = new ShopUserVO();
        BeanUtils.copyProperties(shopUser1,shopUserVO);
        shopUserVO.setGender(shopUser1.getGender() == 0 ? "男" : "女");

        return shopUserVO;
    }

    @Override
    public String modShopUserPwd(String userOldPwd, String userNewPwd, String verifyUserNewPwd, ShopUser shopUser) {

        if (!shopUser.getPassword().equals(EncodeUtils.testDigest(userOldPwd))){
            return "旧密码错误！请重新输入";
        }

        if (StringUtils.isBlank(userNewPwd)){
            return "新密码不能为空！请重新输入";
        }

        //密码格式判断
        if (!Pattern.matches("((^[a-zA-Z]{1,}[0-9]{1,}[a-zA-Z0-9]*)+)"+"|((^[0-9]{1,}[a-zA-Z]{1,}[a-zA-Z0-9]*)+)$",userNewPwd)){
            return "密码格式错误！请重新输入（数字字母组合）";

        }

        if (!userNewPwd.equals(verifyUserNewPwd)){
            return "确认密码不一致！请重新输入";
        }

        shopUser.setPassword(EncodeUtils.testDigest(userNewPwd));
        shopUserMapper.updateById(shopUser);

        //id生成令牌 更新redis
        String userToken = EncodeUtils.testDigest(shopUser.getId());
        redisUtils.set(userToken,shopUser);

        //重新登录
        redisUtils.del(userToken);

        return "密码修改成功，请重新登录！";

    }

    @Override
    public ShopAddressVO addUserAddress(String name,String address,String mobile, Integer status, ShopUser shopUser) {


        ShopAddress shopAddress = new ShopAddress();
        //新增地址
        shopAddress.setId(snowUtil.snowId());
        shopAddress.setName(name);
        shopAddress.setAddress(address);
        shopAddress.setMobile(mobile);
        shopAddress.setUserId(shopUser.getId());

        //查询用户是否有默认地址
        QueryWrapper<ShopAddress> shopUserQueryWrapper = new QueryWrapper<>();
        shopUserQueryWrapper.and(shopUserQueryWrapper1 -> {
            shopUserQueryWrapper1.like("user_id", shopUser.getId());
            shopUserQueryWrapper1.like("status",1);
        });

        //如果有且新增地址为默认地址 改为 非默认地址
        if (shopAddressMapper.selectOne(shopUserQueryWrapper) != null && status == 1){

            //改为 非默认地址
            ShopAddress shopAddress1 = shopAddressMapper.selectOne(shopUserQueryWrapper);
            shopAddress1.setStatus(false);

            shopAddressMapper.updateById(shopAddress1);
        }

        //1 默认地址 0不是默认地址
        if (status == 1){
            shopAddress.setStatus(true);
        }

        //插入数据库
        shopAddressMapper.insert(shopAddress);
        //插入redis
        redisUtils.set(shopAddress.getUserId()+":address:"+shopAddress.getId(),shopAddress);

        //返回前端
        ShopAddressVO shopAddressVO = new ShopAddressVO();
        BeanUtils.copyProperties(shopAddress,shopAddressVO);
        shopAddressVO.setStatus(shopAddress.isStatus() ? "默认地址" : "不是默认地址");

        return shopAddressVO;
    }

    @Override
    public ShopAddressVO modUserAddress(ShopAddress shopAddress) {

        //查询用户是否有默认地址
        QueryWrapper<ShopAddress> shopUserQueryWrapper = new QueryWrapper<>();
        shopUserQueryWrapper.and(shopUserQueryWrapper1 -> {
            shopUserQueryWrapper1.like("user_id", shopAddress.getUserId());
            shopUserQueryWrapper1.eq("status",1);
        });

        //如果有且新增地址为默认地址 改为 非默认地址
        if (shopAddressMapper.selectOne(shopUserQueryWrapper) != null && shopAddress.isStatus()){

            //改为 非默认地址
            ShopAddress shopAddress1 = shopAddressMapper.selectOne(shopUserQueryWrapper);
            shopAddress1.setStatus(false);

            shopAddressMapper.updateById(shopAddress1);
        }

        ShopAddressVO shopAddressVO = new ShopAddressVO();
        shopAddressMapper.updateById(shopAddress);
        BeanUtils.copyProperties(shopAddress,shopAddressVO);
        return shopAddressVO;
    }

    @Override
    public void delUserAddress(List<String> ids) {
        ids.forEach(id -> {
            shopAddressMapper.deleteById(id);
        });
    }
}
